<template>
  <!-- 物流能力 -->
  <div>
    <curd-title v-if="infoFlg" :title="'物流能力'" />
    <avue-crud
      :table-loading="logisticsLoading"
      :option="logisticsOption"
      :data="logisticsList"
      @on-load="onLogisticsLoad"
      @row-save="rowLogisticsSave"
      @row-update="rowLogisticsUpdate"
      @refresh-change="refreshLogisticsChange"
      @row-del="rowLogisticsDel"
      :row-class-name="delRowClassName"
    >
      <template slot="vehicleType" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.vehicleTypeFlag === '1'">{{ scope.row.vehicleType }}</del>
          {{ scope.row.vehicleTypeA }}
        </div>
        <span v-else>{{ scope.row.vehicleType }}</span>
      </template>
      <template slot="quantity" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.quantityFlag === '1'">{{ scope.row.quantity }}</del>
          {{ scope.row.quantityA }}
        </div>
        <span v-else>{{ scope.row.quantity }}</span>
      </template>
      <template slot="logisticsName" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.logisticsNameFlag === '1'">{{ scope.row.logisticsName }}</del>
          {{ scope.row.logisticsNameA }}
        </div>
        <span v-else>{{ scope.row.logisticsName }}</span>
      </template>
    </avue-crud>
  </div>
</template>

<script>
import CurdTitle from '@/components/base/title'
import { logisticsOption } from '@/option/tenant/supplier/equipment-info'
import { getLogisticsList, removeLogistics, addLogistics, updateLogistics } from '@/api/supplier/logistics'
import delRowClassName from '@/views/plugin/workflow/mixins/info'
import { getStore } from '@/util/store'
export default {
  name: 'LogisticsInfo',
  components: { CurdTitle },
  inject: {
    type: {
      default: 'view'
    },
    infoFlg: {
      default: true
    },
    slotFlg: {
      default: false
    }
  },
  mixins: [delRowClassName],
  data() {
    return {
      supplierId: getStore({ name: 'supplierId' }),
      // 物流能力
      logisticsList: [],
      logisticsLoading: false,
      logisticsOption: logisticsOption.call(this)
    }
  },
  methods: {
    onLogisticsLoad() {
      this.infoFlg
        ? ((this.logisticsLoading = true),
          getLogisticsList({ supplierId: this.supplierId }).then(res => {
            this.logisticsList = res.data.data
            this.logisticsLoading = false
          }))
        : ''
    },
    refreshLogisticsChange() {
      this.onLogisticsLoad()
    },
    rowLogisticsSave(row, done, loading) {
      addLogistics({ ...row, supplierId: this.supplierId }).then(
        () => {
          this.onLogisticsLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowLogisticsUpdate(row, index, done, loading) {
      updateLogistics(row).then(
        () => {
          this.onLogisticsLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowLogisticsDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeLogistics(row.id)
        })
        .then(() => {
          this.onLogisticsLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    }
  }
}
</script>

<style></style>
